package com.quanyan.assets.service.impl;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.AssetsPayReceive;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.controller.First;
import com.quanyan.assets.controller.Second;
import com.quanyan.assets.controller.Test;
import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.StateType;
import com.quanyan.assets.entity.TbUserAssets;
import com.quanyan.assets.entity.TbUserAssetsExample;
import com.quanyan.assets.mapper.ETbUserAssetsMapper;
import com.quanyan.assets.mapper.TbUserAssetsMapper;
import com.quanyan.assets.mapper.TbUserShellAccumulateHistoryMapper;
import com.quanyan.assets.request.ReqUserAccumulateModify;
import com.quanyan.assets.service.TestService;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.common.annotation.ServiceExceptionHandler;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.io.IOException;
import java.net.BindException;
import java.util.List;

/**
 * Created by wmb on 2016/6/6.
 */
@Service
public class TestServiceImpl implements TestService{
    @Autowired
    private UserAssetsService userAssetsService;
    @Autowired
    private TbUserShellAccumulateHistoryMapper tbUserShellAccumulateHistoryMapper;
    @Autowired
    private TbUserAssetsMapper tbUserAssetsMapper;
    @Autowired
    private ETbUserAssetsMapper eTbUserAssetsMapper;
    private Logger logger = LoggerFactory.getLogger(TestService.class);
    @Override
    @ServiceExceptionHandler
    public APIResponse<Integer> getAge(APIRequest<Test> test) throws IOException {
        System.out.print(11);

        return APIResponse.returnSuccess(test.getData().getAge());
    }
    @Override
    public void test()  {
        APIRequest apiRequest=new APIRequest();
        ReqUserAccumulateModify reqUserAccumulateModify=new ReqUserAccumulateModify();
        reqUserAccumulateModify.setSystemCode(Constants.USER_CENTER_SYSTEM_CODE);
        reqUserAccumulateModify.setBusinessCode(Constants.USER_CENTER_SHELL_BY_EGGS_RECHANGE_CODE);
        reqUserAccumulateModify.setAssetsType(AssetsType.ASSETS_SHELL.id.byteValue());
        reqUserAccumulateModify.setUid(462);
        reqUserAccumulateModify.setPayReceive(AssetsPayReceive.SYSTEM.getName());
        reqUserAccumulateModify.setReceiver("11111");
        apiRequest.setData(reqUserAccumulateModify);
        APIResponse apiResponse=userAssetsService.modifyAccumulate(apiRequest);
        System.out.println(apiResponse);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int test1() {
        int count= eTbUserAssetsMapper.updateFreezeAccumulate(17,AssetsType.ASSETS_EGGS.id.byteValue(),-20);
        try {
            logger.info("线程等待开始");
            Thread.sleep(10000);
            logger.info("线程等待结束");
        }catch (Exception e){

        }

        return count;
    }

    @Override
    public int test2() {
        TbUserAssetsExample tbUserAssetsExample = new TbUserAssetsExample();
        tbUserAssetsExample.createCriteria().andAssetsTypeEqualTo(AssetsType.ASSETS_EGGS.id.byteValue()).andUidEqualTo(17);
        TbUserAssets tbUserAssets = new TbUserAssets();
        tbUserAssets.setState((byte)0);
        tbUserAssetsMapper.updateByExampleSelective(tbUserAssets,tbUserAssetsExample);
       // int count= eTbUserAssetsMapper.updateFreezeAccumulate(17,AssetsType.ASSETS_EGGS.id.byteValue(),-20);
        return 1;
    }

    @Override
    public TbUserAssets test3() {
        TbUserAssets tbUserAssets = eTbUserAssetsMapper.getUserAssets(17,AssetsType.ASSETS_EGGS.id);
        return tbUserAssets;
    }
}
